//
//  XMUserInfoView.m
//  XMWeibo
//
//  Created by chi on 14-9-2.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMUserInfoView.h"
#import "UIImageView+WebCache.h"

#import "XMUserInfoCountView.h"

#define kEdge (5.0)

@interface XMUserInfoView ()

/** 用户头像 */
@property (nonatomic, weak) UIImageView *iconView;

/** 用户昵称 */
@property (nonatomic, weak) UILabel *screen_name;

/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipPic;

/** 用户个人描述 */
@property (nonatomic, weak) UILabel *descriptionLabel;

/** 微博数 */
@property (nonatomic, weak) XMUserInfoCountView *statuses_count;

/** 关注数 */
@property (nonatomic, weak) XMUserInfoCountView *friends_count;

/** 粉丝数 */
@property (nonatomic, weak) XMUserInfoCountView *followers_count;

/** 分割线 */
@property (nonatomic, weak) UIView *divider;


@end

@implementation XMUserInfoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.userInteractionEnabled = YES;
        self.image = [UIImage stretchableImageWithName:@"common_card_background"];
        
        [self setupTopInfo];
        [self setupBottonInfo];
    }
    return self;
}

- (void)setUser:(XMWeiboUser *)user
{
    _user = user;
    
    // 用户头像
    CGFloat iconViewX = kEdge;
    CGFloat iconViewY = kEdge;
    CGFloat iconViewW = 50.0;
    CGFloat iconViewH = 50.0;
    self.iconView.frame = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default"]];
    
    
    // 用户昵称
    CGFloat screen_nameX = CGRectGetMaxX(self.iconView.frame) + kEdge * 2;
    CGFloat screen_nameY = iconViewY + kEdge;
    CGSize screen_name_size = [user.screen_name sizeWithFont:self.screen_name.font];
    self.screen_name.text = user.screen_name;
    self.screen_name.frame = (CGRect){CGPointMake(screen_nameX, screen_nameY), screen_name_size};
    
    // vip头像
    CGRect vipFrame = self.vipPic.frame;
    vipFrame.origin.x = CGRectGetMaxX(self.screen_name.frame) + kEdge;
    vipFrame.origin.y = kEdge * 2.0 + 0.5;
    self.vipPic.frame = vipFrame;
    
    // 用户个人描述
    CGFloat descriptionX = screen_nameX;
    CGFloat descriptionY = CGRectGetMaxY(self.screen_name.frame) + kEdge;
    CGSize description_size = [user.userDescription sizeWithFont:self.descriptionLabel.font constrainedToSize:CGSizeMake(self.bounds.size.width - descriptionX - 20.0, 30.0)];
    self.descriptionLabel.frame = (CGRect){CGPointMake(descriptionX, descriptionY), description_size};
    self.descriptionLabel.text = user.userDescription;
    
    CGFloat btnW = self.bounds.size.width / 3;
    CGFloat btnY = CGRectGetMaxY(self.iconView.frame) + kEdge;
    
    CGFloat btnH = self.bounds.size.height - btnY;
    
    // 分割线
    self.divider.frame = CGRectMake(2.5f, btnY + kEdge, self.bounds.size.width - 2 * 2.5f, 0.5f);
    
    // 微博数
    self.statuses_count.frame = CGRectMake(0, btnY, btnW, btnH);
    [self.statuses_count setCount:user.statuses_count];
    
    // 关注数
    self.friends_count.frame = CGRectMake(btnW, btnY, btnW, btnH);
    [self.friends_count setCount:user.friends_count];
    
    // 粉丝数
    self.followers_count.frame = CGRectMake(btnW * 2, btnY, btnW, btnH);
    [self.followers_count setCount:user.followers_count];
}

- (void)setupTopInfo
{
    // 用户头像
    UIImageView *iconView = [[UIImageView alloc]init];
//    iconView.layer.cornerRadius = 5.0;
//    iconView.layer.masksToBounds = YES;
    self.iconView = iconView;
    [self addSubview:iconView];
    
    UIImageView *smallIcon = [[UIImageView alloc]init];
    smallIcon.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
    CGFloat smallIconWH = 18.0f;
    
    smallIcon.frame = CGRectMake(50.0 - smallIconWH - 1.0, 50.0 - smallIconWH + 5.0, smallIconWH, smallIconWH);
    [iconView addSubview:smallIcon];
    
    // 用户昵称
    UILabel *screen_name = [[UILabel alloc]init];
    screen_name.font = [UIFont systemFontOfSize:14.0];
    self.screen_name = screen_name;
    [self addSubview:screen_name];
    
    // 会员图标
    UIImageView *vipPic = [[UIImageView alloc]init];
    vipPic.image = [UIImage imageNamed:@"common_icon_membership"];
    vipPic.bounds = CGRectMake(0.0, 0.0, 14.0, 14.0);
    self.vipPic = vipPic;
    [self addSubview:vipPic];
    
    // 用户个人描述
    UILabel *descriptionLabel = [[UILabel alloc]init];
    descriptionLabel.numberOfLines = 0;
    descriptionLabel.font = [UIFont systemFontOfSize:12.0];
    descriptionLabel.textColor = [UIColor grayColor];
    self.descriptionLabel = descriptionLabel;
    [self addSubview:descriptionLabel];
}

- (void)setupBottonInfo
{
    // 分割线
    UIView *divider = [[UIView alloc]init];
    self.divider = divider;
    divider.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:divider];
    
    // 微博数
    XMUserInfoCountView *statuses_count = [XMUserInfoCountView buttonWithType:UIButtonTypeCustom];
    [statuses_count setText:@"微博"];
    self.statuses_count = statuses_count;
    [self addSubview:statuses_count];
    
    // 关注数
    XMUserInfoCountView *friends_count = [XMUserInfoCountView buttonWithType:UIButtonTypeCustom];
    [friends_count setText:@"关注"];
    self.friends_count = friends_count;
    [self addSubview:friends_count];
 
    // 粉丝数
    XMUserInfoCountView *followers_count = [XMUserInfoCountView buttonWithType:UIButtonTypeCustom];
    [followers_count setText:@"粉丝"];
    self.followers_count = followers_count;
    [self addSubview:followers_count];
   
}

@end
